package com.expert.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.expert.Mapper.ExtractMapper;
import com.expert.dto.AddConditionDTO;
import com.expert.dto.ExtractOther;
import com.expert.dto.ExtractOthers;
import com.expert.enums.ProjectStatus;
import com.expert.exception.AddException;
import com.expert.pojo.*;
import com.expert.service.*;
import com.expert.vo.ExtractInitVo;
import com.expert.vo.RemoteExpertVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName: ExtractServiceImpl
 * @Author: cxs
 * @Date: 2024/03/21 21:02
 * @Description:
 **/
@Service

public class ExtractServiceConditionImpl extends ServiceImpl<ExtractMapper, ExtractCondition> implements ExtractConditionService {


    @Autowired
    UserService userService;
    @Autowired
    ProjectService projectService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    ExpertService expertService;
    @Override
    public ExtractInitVo getConditionInfo(Long id) {
        List<ExtractCondition> extractList = this.lambdaQuery().eq(ExtractCondition::getProjectId, id).list();
        ExtractInitVo extractInitVo=new ExtractInitVo();
        if (extractList!=null&&extractList.size()>0){
            extractInitVo.setHasData(true);
            ArrayList<RemoteExpertVo> remoteExpertVos=new ArrayList<>();
            ArrayList<ExtractOther> others=new ArrayList<>();
            extractList.forEach(extract -> {
                if (extract.getExtractDefaultExpert()!=null){
                    RemoteExpertVo remoteExpertVo=new RemoteExpertVo();
                    remoteExpertVo.setId(extract.getExtractDefaultExpert());
                    remoteExpertVo.setName(extract.getExtractDefaultExpertName());
                    remoteExpertVo.setIdNumber(extract.getExtractDefaultExpertIdNumber().substring(12));
                    remoteExpertVos.add(remoteExpertVo);
                }
                if (extract.getExtractCategory()!=null||extract.getExpertTitle()!=null){
                    ExtractOther extractOther=new ExtractOther();
                    BeanUtils.copyProperties(extract,extractOther);
                    others.add(extractOther);
                }

            });
            extractInitVo.setOthers(others);
            extractInitVo.setDefaultExpert(remoteExpertVos);
        }
        return extractInitVo;
    }

    /**
     * 添加抽取条件
     *
     * @param addConditionDTO
     */
    @Override
    @Transactional
    public void addCondition(AddConditionDTO addConditionDTO) {
        Long projectId = addConditionDTO.getProjectId();
        Project project = this.projectService.getById(projectId);
        ArrayList<ExtractCondition> extractConditions=new ArrayList<>();
        List<Long> extractDefaultExpert = addConditionDTO.getExtractDefaultExpert();
        AtomicInteger num= new AtomicInteger();
        extractDefaultExpert.forEach(expert->{
            Expert expertInfo = this.expertService.getById(expert);
            ExtractCondition extractCondition=new ExtractCondition();
            extractCondition.setExtractDefaultExpert(expert);
            extractCondition.setExtractDefaultExpertIdNumber(expertInfo.getExpertIdNumber());
            extractCondition.setExtractDefaultExpertName(expertInfo.getExpertName());
            extractCondition.setProjectId(projectId);
            num.getAndIncrement();
            extractConditions.add(extractCondition);
        });
        List<ExtractOthers> others = addConditionDTO.getOthers();
        others.forEach(other->{
            ExtractCondition extractCondition=new ExtractCondition();
            if (other.getExtractNumber()>0&&(other.getExtractCategory()!=null||other.getExpertTitle()!=null)){
                num.getAndAdd(other.getExtractNumber());
                extractCondition.setExpertTitle(other.getExpertTitle());
                extractCondition.setExtractNumber(other.getExtractNumber());
                Long extractCategory = other.getExtractCategory();
                if (extractCategory!=null){
                    Category category = this.categoryService.getById(extractCategory);
                    extractCondition.setExtractCategory(extractCategory);
                    extractCondition.setExtractCategoryName(category.getCategoryCode()+" "+category.getCategoryName());
                }
                extractConditions.add(extractCondition);
                extractCondition.setProjectId(projectId);
            }
        });
        if (num.get()!=project.getProjectReviewNumber()){
            System.out.println(num.get());
            throw new AddException("人数与计划人数不符");
        }
        //删除原来的信息
        List<Long> ids = this.lambdaQuery().eq(ExtractCondition::getProjectId, projectId).list().stream().map(ExtractCondition::getId).toList();
        this.removeBatchByIds(ids);
        this.saveBatch(extractConditions);
        project.setProjectStatus(ProjectStatus.NOT_DRAWN.getCode());
        this.projectService.updateById(project);
    }
}
